# -*- coding:utf-8 -*-
# file:weather.py

import re
import urllib
import sys
import time


class Weather():
    def __init__(self, name, uri):
        self.city_name = name
        self.city_uri = uri

    def GetWeather(self):
        try:
            # 获取网页源文件
            sock = urllib.urlopen(self.city_uri)
            strhtml = sock.read()
            strhtml = unicode(strhtml, 'gb2312','ignore').encode('utf-8','ignore')

            # 正则式取温度信息
            theGrades = re.findall('''(-?\d+)℃''', strhtml)

            # 获取天气描述信息
            weathers = re.findall('''<br/>(.*)</td>''',strhtml)

            # 定义时间格式
            this_date = str(time.strftime("%Y/%m/%d %a"))
            now = int(time.time())
            sec = 24*60*60
            day_tommo = "明天(%s号)" % str(time.strftime("%d", time.localtime(now+1*sec)))
            day_aftom = "后天(%s号)" % str(time.strftime("%d", time.localtime(now+2*sec)))

            # 定义短信正文
            sms = ["%s" % self.city_name]
            sms.append("%s: %s, %s～%s℃" % (day_tommo, weathers[1], theGrades[3], theGrades[2]))
            sms.append("%s: %s, %s～%s℃" % (day_aftom, weathers[2], theGrades[5], theGrades[4]))
            smscontent = "\n".join(sms)

            return smscontent

        except:
            return "出问题了，没有抓到气象信息 =.=!"
